package cn.tedu.dbinit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.jdbc.datasource.init.ScriptUtils;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;


import java.sql.SQLException;


@SpringBootApplication
public class DbInitApplication {
	@Autowired
	private DataSource dataSource;
	public static void main(String[] args) {
		SpringApplication.run(DbInitApplication.class, args);
	}

	@PostConstruct
	public void init() throws SQLException {
		exec("sql/account.sql");
		exec("sql/storage.sql");
		exec( "sql/order.sql");
		exec("sql/seata-server.sql");
	}

	private void exec(String script) throws SQLException {
		ClassPathResource r = new ClassPathResource(script, DbInitApplication.class.getClassLoader());
		EncodedResource er = new EncodedResource(r, "utf-8");
		ScriptUtils.executeSqlScript(dataSource.getConnection(), er);
	}
}
